package com.yinhai.mycat_test.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yinhai.mycat_test.VO.InserOrderItemVO;
import com.yinhai.mycat_test.VO.InsertOrderVO;
import com.yinhai.mycat_test.VO.OrderVO;
import com.yinhai.mycat_test.VO.PageVO;
import com.yinhai.mycat_test.entity.Order;
import com.yinhai.mycat_test.entity.OrderItem;
import com.yinhai.mycat_test.mapper.read.OrderItemReadMapper;
import com.yinhai.mycat_test.service.read.OrderReadService;
import com.yinhai.mycat_test.service.write.OrderWriteService;

import io.swagger.models.auth.In;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderReadService orderReadService;
    
    @Autowired
    private OrderItemReadMapper orderItemReadMapper;
    
    @Autowired
    private OrderWriteService orderWriteService;
    
    
    /**
     * 根据用户id插入订单和订单详情
     *
     * @param insertOrderVO
     * @return
     */
    @PostMapping("/insertOrderByUserId")
    public String insertOrderByUserId(@RequestBody InsertOrderVO insertOrderVO){
        return orderWriteService.insertOrderByUserId(insertOrderVO);
    }
    
    
    /**
     * 分页显示所有订单
     *
     * @param pageVo
     * @return
     */
    @PostMapping("/listOrdersByPage")
    public IPage<Order> listOrders(@RequestBody PageVO pageVo){
        return orderReadService.listAllOrder(pageVo);
    }
    
    /**
     * 分页显示某个用户的订单
     *
     * @param orderVO
     * @return
     */
    @PostMapping("/listOrdersByUserIdbyPage")
    public IPage<Order> listOrdersByUserId(@RequestBody OrderVO orderVO){
        return orderReadService.listOrderByUserId(orderVO);
    }
    
    /**
     * 通过订单id查询订单详情
     *
     * @param orderId
     * @return
     */
    @GetMapping(value = "/querOrderByOrderId")
    public Order querOrderByOrderId( Long orderId){
        return orderReadService.querOrderByOrderId(orderId);
    }
    
    /**
     * 通过订单id查询订单商品详细信息
     *
     * @param orderId
     * @return
     */
    @GetMapping("/querOrderDetailByOrderId")
    public List<OrderItem> querOrderDetailByOrderId(Long orderId){
        return orderItemReadMapper.queryOrderItemByOrderId(orderId);
    }
    
    /**
     * 根据订单id模糊查询订单记录
     *
     * @param orderId
     * @return
     */
    @GetMapping("/queryOrderLikeOrderId")
    public List<Order> queryOrderLikeOrderId(Long orderId){
       return orderReadService.queryOrderLikeOrderId(orderId);
    }
    
    /**
     * 通过商品名称模糊查询订单
     */
    @PostMapping("/queryOrderLikeOrderItemName")
    public List<Order> queryOrderLikeOrderItemName(@RequestBody OrderVO orderVO){
        return orderReadService.queryOrderLikeOrderItemName(orderVO);
    }
    
    /**
     * 查询订单数量
     * */
    @GetMapping("/queryOrderCount")
    public Integer queryOrderCount(){
        return orderReadService.queryOrderCount();
    }
    
    /**
     * 根据用户id查询用户所有订单数量
     *
     * @param userId
     * @return
     */
    @GetMapping("/queryOrderCountByUserId")
    public Integer queryOrderCountByUserId(Long userId){
        return orderReadService.queryOrderCountByUserId(userId);
    }
    
    /**
     * 通过订单id和用户id删除订单
     *
     * @param orderId
     * @param userId
     * @return
     */
    @PostMapping("/deleteOrderByOrderIdAndUserId")
    public String deleteOrderByOrderIdAndUserId(Long orderId,Long userId){
        return orderWriteService.deleteOrderByOrderId(userId,orderId);
    }
    
    /**
     * 通过订单id列表和用户id批量删除订单和订单详情
     *
     * @param orderIds
     * @param userId
     * @return
     */
    @PostMapping("/deleteOrderListByOrderIdAndUserId")
    public String deleteOrderListByOrderIdAndUserId(@RequestBody List<Long> orderIds,Long userId){
        return orderWriteService.deleteOrderListByOrderIdAndUserId(userId,orderIds);
    }
    
    /**
     * 清空订单列表和订单详情
     * @return
     */
    @PostMapping("/truncateOrderAndOrderDetail")
    public String emptyOrder(){
        return orderWriteService.emptyOrder();
    }
    
    /**
     * 批量新增订单数量,可以控制新增订单数量,以及订单详情数量
     *
     * @param orderCount
     * @param orderItemCount
     * @return
     */
    @PostMapping("/batchInsertOrder")
    public String batchInsertOrder(Integer orderCount,Integer orderItemCount){
        if(orderCount < 1){
            throw new RuntimeException("invalid orderCount,orderCount must be bigger than 1");
        }
        if(orderItemCount < 1){
            throw new RuntimeException("invalid orderItemCount,orderItemCount must be bigger than 1");
        }
        for (int i=0;i < orderCount;i++){
            InsertOrderVO insertOrderVO = new InsertOrderVO();
            insertOrderVO.setAddress("addres:"+i);
            insertOrderVO.setTotalPrice(Float.valueOf(i));
            insertOrderVO.setUserId(i);
            List<InserOrderItemVO> orderItemVOList = new ArrayList<>(orderItemCount);
            for (int k = 0;k < orderItemCount;k++){
                InserOrderItemVO itemVO = new InserOrderItemVO();
                itemVO.setItemCount(k);
                itemVO.setItemId(k);
                itemVO.setItemName("item name:"+k);
                itemVO.setItemPrice(Float.valueOf(i*k));
                orderItemVOList.add(itemVO);
            }
            insertOrderVO.setItemList(orderItemVOList);
            orderWriteService.insertOrderByUserId(insertOrderVO);
        }
        return "order success batch insert:order count="+orderCount+",orderItemCount="+orderItemCount;
    }
}
